package main

import (
	"etcdkeeper/keeper"
	"flag"
	"fmt"
	"log"
	"os"
	"os/signal"
	"strconv"
	"strings"

	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
)

var (
	version  string
	commitID string

	v = flag.Bool("v", false, "show application version")
)

func main() {
	flag.CommandLine.Parse(os.Args[1:])
	if *v {
		fmt.Printf("%s %s\n", strings.TrimSpace(version), strings.TrimSpace(commitID))
		return
	}

	err := keeper.Init()
	if err != nil {
		return
	}

	gin.SetMode("debug")

	ginRouter := gin.New()
	pprof.Register(ginRouter)
	keeper.SetRouter(ginRouter)

	log.Printf("listening on %s:%d\n", *keeper.Host, *keeper.Port)

	err = ginRouter.Run(*keeper.Host + ":" + strconv.Itoa(*keeper.Port))
	if err != nil {
		log.Printf("server etcdkeeper error: %v", err)
		return
	}

	log.Printf("server etcdkeeper start")

	signals := make(chan os.Signal, 1)
	signal.Notify(signals, os.Interrupt)

	for range signals {
		log.Printf("service shutdown by interupt")
		return
	}
}
